<template>
  <link
    rel="stylesheet"
    href="https://at.alicdn.com/t/c/font_4682916_eus4j1hjo3.css"
  />
  <div class="box">
    <h2>todos</h2>
    <div class="header">
      <input
        type="text"
        @keyup.enter="addMsg"
        v-model="newTodo"
        placeholder="请输入代办事项"
      />
    </div>

    <div class="content">
      <ul>
        <li v-for="(item, index) in todos" :key="item.id">
          <div>
            <input type="checkbox" v-model="item.isHidden" />
            <span :class="{ line: item.isHidden }">{{ item.content }}</span>
          </div>

          <div
            v-show="item.isHidden"
            @click="deleteMsg(index)"
            class="iconfont icon-a-11-Xhao01"
          ></div>
        </li>
      </ul>
      <div class="footer" v-if="todos.length !== 0">
        <span>已完成:{{ Count }}/总数:{{ AllCount }}</span>

        <div class="delete-btn">
          <button @click="removeMsg">删除已完成</button>
          <button @click="togetherAll">全选</button>
        </div>
      </div>
      <h2 v-else>暂无任务</h2>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch, onMounted } from "vue";
const newTodo = ref("");
const todos = ref([]);
let isAllChecked = false;

onMounted(() => {
  todos.value = JSON.parse(
    localStorage.getItem("todos") ? localStorage.getItem("todos") : "[]"
  );
});

watch(
  todos,
  () => {
    localStorage.setItem("todos", JSON.stringify(todos.value));
  },
  { deep: true }
);

// todo完成个数
const Count = computed(() => {
  return todos.value.filter((item) => item.isHidden).length;
});

// 总数
const AllCount = computed(() => todos.value.length);

// 全选
const togetherAll = () => {
  isAllChecked = !isAllChecked;
  todos.value.forEach((todos) => (todos.isHidden = isAllChecked));
};

// 删除已完成
const removeMsg = () => {
  todos.value = todos.value.filter((item) => !item.isHidden);
};

// 添加
const addMsg = () => {
  // 给用户提示
  if (newTodo.value == "") {
    alert("你还没有输入代办事项!");
    return;
  }
  todos.value.unshift({
    id: Date.now(),
    content: newTodo.value,
    isHidden: false,
  });
  newTodo.value = "";
  return;
};

// 删除
const deleteMsg = (index) => {
  if (confirm(`确定要删除内容为[${todos.value[index].content}]的代办事项吗?`)) {
    todos.value.splice(index, 1);
  }
  return;
};
</script>

<style  scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-size: 20px;
}
.iconfont {
  font-size: 25px;
}
.box {
  width: 400px;
  margin: 20px auto;
}
.box h2 {
  text-align: center;
}
.box h2:last-child {
  color: red;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.header input {
  width: 100%;
  height: 30px;
  border: 1px solid skyblue;
  border-radius: 10px;
  outline: none;
}
ul {
  list-style: none;
  padding: 0;
}
li {
  margin: 10px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
button {
  margin-left: 20px;
}
ul li div:last-child {
  color: red;
  cursor: pointer;
}
.content input {
  width: 18px;
  height: 18px;
  margin-right: 20px;
}
.content .hint {
  color: red;
  margin: 10px;
}
.footer {
  display: flex;
  justify-content: space-between;
  cursor: pointer;
}
.delete-btn button {
  cursor: pointer;
  font-size: 18px;
}
.line {
  text-decoration: line-through;
  color: grey;
}
</style>